package models;

import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * User: gipxy
 * Date: 4/21/12
 */
@Entity
public class CustomerOrder extends Model {

    @Id
    public Long id;


    public String soDonHang;

    @Formats.DateTime(pattern="dd/MM/yyyy")
    public Date ngayDatHang;

    @ManyToOne
    public CustomerInformation khachHang;

    @OneToMany(mappedBy="customerOrder")
    public List<ProductOfOrder> allProduct;

    public static Finder<Long,CustomerOrder> find = new Finder<Long,CustomerOrder>(Long.class, CustomerOrder.class);

    public static List<CustomerOrder> all(){
        List<CustomerOrder> tasks = CustomerOrder.find.all();
        return tasks;
    }
    public static Page<CustomerOrder> page(int page, int pageSize, String sortBy, String order, String filter) {

        return find.where()
                .ilike("soDonHang", "%" + filter + "%")
                .orderBy(sortBy + " " + order)
                .findPagingList(pageSize)
                .getPage(page);
    }

    public static void addProductinOrder(String code,String name, String soLuong, String donGia ){

    }

    public CustomerOrder(String soDonHang) {
        this.soDonHang = soDonHang;
    }

    public CustomerOrder() {
    }
    public String getNgayDatHangAsString(){
        return new SimpleDateFormat("dd/MM/yyyy").format(ngayDatHang);
    }
}
